//
//  HNShareController.m
//  shezhan
//
//  Created by 朱传正 on 2016/10/26.
//  Copyright © 2016年 haoniu. All rights reserved.
//

#import "HNShareController.h"
#import <ShareSDK/ShareSDK.h>
#import <ShareSDKUI/ShareSDK+SSUI.h>

@interface HNShareController ()
/*广告图*/
@property (weak, nonatomic) IBOutlet UIImageView *adImageView;
/*广告描述*/
@property (weak, nonatomic) IBOutlet UILabel *adLabel;
/*标题*/
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;

@property (weak, nonatomic) IBOutlet UILabel *shareTagLabel;

@property (weak, nonatomic) IBOutlet UILabel *weiXinLabel;

@property (weak, nonatomic) IBOutlet UILabel *qqLabel;



@end

@implementation HNShareController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.titleLabel.text = @"分享APP";
    
    self.navigationController.navigationBar.titleTextAttributes = @{UITextAttributeTextColor: RGB(114, 114, 114),
                                                                    UITextAttributeFont : [UIFont boldSystemFontOfSize:16]};
    [self.titleLabel setFont:[UIFont systemFontOfSize:16]];
    [self.titleLabel setTextColor:RGB(114,114,114)];
    
    //改变分享文字
    [self changeShareText];
    
    [self.adLabel setTextColor:RGB(136, 136, 136)];
    [self.shareTagLabel setTextColor:RGB(154, 154, 154)];
    [self.weiXinLabel setTextColor:RGB(154, 154, 154)];
    [self.qqLabel setTextColor:RGB(154, 154, 154)];
    
}

-(void)goBackAction{
    [self.navigationController popViewControllerAnimated:YES];
}


- (IBAction)shareWeiXin:(id)sender {
    NSString * shareDetail = @"时尚潮流APP，欢迎下载。";//@"分享内容"
    
    
    //    NSString * infoURL = [NSString stringWithFormat:@"%@%@?prodId=%@",_BaseURL,url_shareURL,_productId];
     NSString * infoURL = [NSString stringWithFormat:@"%@",url_ShareAPP];
    NSLog(@"---------- infoURL = %@",infoURL);
    
    NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
    [shareParams SSDKSetupShareParamsByText:shareDetail
                                     images:[UIImage imageNamed:@"Icon-80.png"]
                                        url:[NSURL URLWithString:infoURL]
                                      title:str_ShareAPP
                                       type:SSDKContentTypeAuto];
    //2、分享（可以弹出我们的分享菜单和编辑界面）
    [ShareSDK share:SSDKPlatformSubTypeWechatSession parameters:shareParams onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) {
        NSLog(@"state = %ld userData = %@,%@",state,userData,error);
        
        switch (state) {
            case SSDKResponseStateSuccess:
            {
                UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"分享成功" message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
                [alertView show];
                break;
            }
            case SSDKResponseStateFail:
            {
                NSDictionary * err = error.userInfo;
                NSString * info = [err objectForKey:@"error description "];
                info = info==nil?@"":info;
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"分享失败" message:info delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                NSLog(@"%@",info);
                [alert show];
                break;
            }
            default:
                break;
        }
        
    }];
}

- (IBAction)shareQQ:(id)sender {
    NSString * shareDetail = @"时尚潮流APP，欢迎下载。";//@"分享内容"
    
    
    //    NSString * infoURL = [NSString stringWithFormat:@"%@%@?prodId=%@",_BaseURL,url_shareURL,_productId];
    NSString * infoURL = [NSString stringWithFormat:@"%@",url_ShareAPP];
    NSLog(@"---------- infoURL = %@",infoURL);
    
    NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
    [shareParams SSDKSetupShareParamsByText:shareDetail
                                     images:[UIImage imageNamed:@"Icon-180.png"]
                                        url:[NSURL URLWithString:infoURL]
                                      title:str_ShareAPP
                                       type:SSDKContentTypeAuto];
    //2、分享（可以弹出我们的分享菜单和编辑界面）
    [ShareSDK share:SSDKPlatformSubTypeQQFriend parameters:shareParams onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) {
        NSLog(@"state = %ld userData = %@,%@",state,userData,error);
        
        switch (state) {
            case SSDKResponseStateSuccess:
            {
                UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"分享成功" message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
                [alertView show];
                break;
            }
            case SSDKResponseStateFail:
            {
                NSDictionary * err = error.userInfo;
                NSString * info = [err objectForKey:@"error description "];
                info = info==nil?@"":info;
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"分享失败" message:info delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                NSLog(@"%@",info);
                [alert show];
                break;
            }
            default:
                break;
        }
        
    }];

}


- (IBAction)shareXinLang:(id)sender {
    NSString * shareDetail = @"时尚潮流APP，欢迎下载。";//@"分享内容"
    
    
    //    NSString * infoURL = [NSString stringWithFormat:@"%@%@?prodId=%@",_BaseURL,url_shareURL,_productId];
    NSString * infoURL = [NSString stringWithFormat:@"%@",url_ShareAPP];
    NSLog(@"---------- infoURL = %@",infoURL);
    
    NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
    [shareParams SSDKSetupShareParamsByText:shareDetail
                                     images:[UIImage imageNamed:@"Icon-180.png"]
                                        url:[NSURL URLWithString:infoURL]
                                      title:str_ShareAPP
                                       type:SSDKContentTypeAuto];
    //2、分享（可以弹出我们的分享菜单和编辑界面）
    [ShareSDK share:SSDKPlatformTypeSinaWeibo  parameters:shareParams onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) {
        NSLog(@"state = %ld userData = %@,%@",state,userData,error);
        
        switch (state) {
            case SSDKResponseStateSuccess:
            {
                UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"分享成功" message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
                [alertView show];
                break;
            }
            case SSDKResponseStateFail:
            {
                NSDictionary * err = error.userInfo;
                NSString * info = [err objectForKey:@"error description "];
                info = info==nil?@"":info;
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"分享失败" message:info delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                NSLog(@"%@",info);
                [alert show];
                break;
            }
            default:
                break;
        }
        
    }];
    
}

- (IBAction)friendLang:(id)sender {
    NSString * shareDetail = @"时尚潮流APP，欢迎下载。";//@"分享内容"
    
    
    //    NSString * infoURL = [NSString stringWithFormat:@"%@%@?prodId=%@",_BaseURL,url_shareURL,_productId];
      NSString * infoURL = [NSString stringWithFormat:@"%@",url_ShareAPP];
    NSLog(@"---------- infoURL = %@",infoURL);
    
    NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
    [shareParams SSDKSetupShareParamsByText:shareDetail
                                     images:[UIImage imageNamed:@"Icon-180.png"]
                                        url:[NSURL URLWithString:infoURL]
                                      title:str_ShareAPP
                                       type:SSDKContentTypeAuto];
    //2、分享（可以弹出我们的分享菜单和编辑界面）
    [ShareSDK share:SSDKPlatformSubTypeWechatTimeline parameters:shareParams onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) {
        NSLog(@"state = %ld userData = %@,%@",state,userData,error);
        
        switch (state) {
            case SSDKResponseStateSuccess:
            {
                UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"分享成功" message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
                [alertView show];
                break;
            }
            case SSDKResponseStateFail:
            {
                NSDictionary * err = error.userInfo;
                NSString * info = [err objectForKey:@"error description "];
                info = info==nil?@"":info;
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"分享失败" message:info delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                NSLog(@"%@",info);
                [alert show];
                break;
            }
            default:
                break;
        }
        
    }];
    
}


- (IBAction)backBtn:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}


#pragma  mark - 改变分享文字
-(void)changeShareText{
    NSString * loginName = [NSString stringWithFormat:@"%@",[AppConfig getUserInfoByKey:@"hnLoginName"]];
    
    NSMutableDictionary * param = [NSMutableDictionary dictionaryWithObjectsAndKeys:loginName,@"hnLoginName", nil];

    
    NSLog(@"==param:%@",param);
    
    
    AFHTTPSessionManager *manager = [[AFHTTPSessionManager manager] initWithBaseURL:[NSURL URLWithString:url_Base]];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager POST:url_ShareText parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSError * error;
        NSDictionary * data=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:&error];
        [self hideHud];
        if (error) {
            NSLog(@"====error1:%@",error);
            [self showHint:@"获取详情失败"];
            return;
        }
        NSLog(@"==dict:%@",data);
        NSString * info = [NSString stringWithFormat:@"%@",[data objectForKey:@"message"]];
        if (![AppConfig isNULL:info]) {
            
            self.adLabel.text = info;
            
        }else{
            NSString * info = [NSString stringWithFormat:@"%@",[data objectForKey:@"message"]];
            [self showHint:info];
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull   error) {
        NSLog(@"====error:%@",error);
        [self hideHud];
        [self showHint:@"分享详情获取失败"];
    }];


}



@end
